package cn.sinata.wanglin.mine.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Lableinfo;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

public class AlterLableActivity extends TitleActivity {
    @BindView(R.id.rv_blonglable)
    RecyclerView rv_blonglable;//
    BaseRecyclerAdapter blongLableAdapter;
    @BindView(R.id.rv_alllable)
    RecyclerView rv_alllable;//所有的标签
    BaseRecyclerAdapter alllableAdapter;
    @BindView(R.id.rv_currentlable)
    RecyclerView rv_currentlable;//当前选择的标签
    BaseRecyclerAdapter currentlableAdapter;
    private ArrayList<Lableinfo> mBelongLabledatas=new ArrayList<>();
    private ArrayList<Lableinfo> malllabledatas=new ArrayList<>();
    private ArrayList<Lableinfo> mcurrentlabledatas=new ArrayList<>();
    private String  belonglable;
    @Override
    protected int setContentId() {
        return R.layout.activity_alter_lable;
    }
    @Override
    protected void initView() {
        super.initView();
        setTitle("我的标签");
        Bundle bundle=getIntent().getExtras();
        if(bundle!=null){
            belonglable=bundle.getString("belonglable");
            ArrayList<Lableinfo> mTemp=new ArrayList<>();
            mTemp= (ArrayList<Lableinfo>) bundle.getSerializable("otherlable");
            if(mTemp!=null&&mTemp.size()!=0){
                mcurrentlabledatas.addAll(mTemp);
            }

        }
        if(blongLableAdapter==null){
            mBelongLabledatas.add(new Lableinfo("单身贵族",false));
            mBelongLabledatas.add(new Lableinfo("摄影师",false));
            mBelongLabledatas.add(new Lableinfo("宠物控",false));
            mBelongLabledatas.add(new Lableinfo("旅游",false));
            mBelongLabledatas.add(new Lableinfo("游戏",false));
            mBelongLabledatas.add(new Lableinfo("妈妈",false));
            mBelongLabledatas.add(new Lableinfo("电脑迷",false));
            mBelongLabledatas.add(new Lableinfo("运动",false));
            mBelongLabledatas.add(new Lableinfo("跑步",false));
            mBelongLabledatas.add(new Lableinfo("艺术家",false));
            mBelongLabledatas.add(new Lableinfo("棋牌控",false));
            mBelongLabledatas.add(new Lableinfo("歌手",false));
            mBelongLabledatas.add(new Lableinfo("小说迷",false));
            mBelongLabledatas.add(new Lableinfo("活动迷",false));
            mBelongLabledatas.add(new Lableinfo("烹饪",false));
            mBelongLabledatas.add(new Lableinfo("二次元",false));
            mBelongLabledatas.add(new Lableinfo("爱逛街",false));
            mBelongLabledatas.add(new Lableinfo("电影迷",false));
            mBelongLabledatas.add(new Lableinfo("吃货",false));
            mBelongLabledatas.add(new Lableinfo("其它",false));
            for (int i=0;i<mBelongLabledatas.size();i++){
             if(mBelongLabledatas.get(i).getName().equals(belonglable)){
                 mBelongLabledatas.get(i).setIschecked(true);
             }

            }
            GridLayoutManager mgr=new GridLayoutManager(this,4);
            rv_blonglable.setLayoutManager(mgr);
            blongLableAdapter=new BaseRecyclerAdapter<Lableinfo>(mBelongLabledatas,R.layout.item_belonglable) {
                @Override
                public void onBind(final int position, final Lableinfo data, ViewHolder holder) {
                    holder.setText(R.id.cb_choicelable,data.getName());
                    ((CheckBox)holder.bind(R.id.cb_choicelable)).setChecked(data.ischecked());
                    holder.bind(R.id.cb_choicelable).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            for(int i=0;i<mBelongLabledatas.size();i++){
                                mBelongLabledatas.get(i).setIschecked(false);
                            }
                            CheckBox mCheckBox= (CheckBox)view;
                            mCheckBox.setChecked(true);
                            mBelongLabledatas.get(position).setIschecked(true);
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            rv_blonglable.setAdapter(blongLableAdapter);
        }
        malllabledatas.add(new Lableinfo("单身贵族",false));
        malllabledatas.add(new Lableinfo("摄影师",false));
        malllabledatas.add(new Lableinfo("宠物控",false));
        malllabledatas.add(new Lableinfo("旅游",false));
        malllabledatas.add(new Lableinfo("游戏",false));
        malllabledatas.add(new Lableinfo("妈妈",false));
        malllabledatas.add(new Lableinfo("电脑迷",false));
        malllabledatas.add(new Lableinfo("运动",false));
        malllabledatas.add(new Lableinfo("跑步",false));
        malllabledatas.add(new Lableinfo("艺术家",false));
        malllabledatas.add(new Lableinfo("棋牌控",false));
        malllabledatas.add(new Lableinfo("歌手",false));
        malllabledatas.add(new Lableinfo("小说迷",false));
        malllabledatas.add(new Lableinfo("活动迷",false));
        malllabledatas.add(new Lableinfo("烹饪",false));
        malllabledatas.add(new Lableinfo("二次元",false));
        malllabledatas.add(new Lableinfo("爱逛街",false));
        malllabledatas.add(new Lableinfo("电影迷",false));
        malllabledatas.add(new Lableinfo("吃货",false));
        malllabledatas.add(new Lableinfo("其它",false));
        for(int i=0;i<mcurrentlabledatas.size();i++){
            for(int j=0;j<malllabledatas.size();j++){
                if(mcurrentlabledatas.get(i).getName().equals(malllabledatas.get(j).getName())){
                    malllabledatas.get(j).setIschecked(true);
                }
            }

        }
        if(alllableAdapter==null){
            GridLayoutManager mgr=new GridLayoutManager(this,4);
            rv_alllable.setLayoutManager(mgr);
            alllableAdapter=new BaseRecyclerAdapter<Lableinfo>(malllabledatas,R.layout.item_alllable) {
                @Override
                public void onBind(final int position, final Lableinfo data, ViewHolder holder) {
                     holder.setText(R.id.cb_alllable,data.getName());
                    ((CheckBox)holder.bind(R.id.cb_alllable)).setChecked(data.ischecked());
                    ((CheckBox)holder.bind(R.id.cb_alllable)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            CheckBox  mCheckBox=(CheckBox)view;
                            if(mCheckBox.isChecked()){
                                int checknum=0;
                                for(int i=0;i<malllabledatas.size();i++){
                                  if(malllabledatas.get(i).ischecked()){
                                      checknum++;
                                  }
                                }
                                if(checknum==3){
                                    showToast("最多选择3个");
                                    mCheckBox.setChecked(false);
                                    return;
                                }else{
                                    malllabledatas.get(position). setIschecked(true);
                                    mcurrentlabledatas.add(data);
                                    currentlableAdapter.notifyDataSetChanged();
                                    alllableAdapter.notifyDataSetChanged();
                                }
                            }else{
                                mCheckBox.setChecked(true);
                            }
                        }
                    });
                }
            };
            rv_alllable.setAdapter(alllableAdapter);
        }
        if(currentlableAdapter==null){
            GridLayoutManager mgr=new GridLayoutManager(this,4);
            rv_currentlable.setLayoutManager(mgr);
            currentlableAdapter=new BaseRecyclerAdapter<Lableinfo>(mcurrentlabledatas,R.layout.item_currentlable) {
                @Override
                public void onBind(final int position, final Lableinfo data, ViewHolder holder) {
                    holder.setText(R.id.cb_currentlable,data.getName());
                    ((ImageView) holder.bind(R.id.iv_currentlable_delete)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String deletelable= mcurrentlabledatas.get(position).getName();
                            for(int i=0;i<malllabledatas.size();i++){
                                if(deletelable.equals(malllabledatas.get(i).getName())){
                                    malllabledatas.get(i).setIschecked(false);
                                    alllableAdapter.notifyDataSetChanged();
                                }
                            }
                            mcurrentlabledatas.remove(position);
                            currentlableAdapter.notifyDataSetChanged();
                        }
                    });
                }
            };
            rv_currentlable.setAdapter(currentlableAdapter);
        }

    }

    @OnClick({R.id.tv_surelable})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_surelable:
                //描述:确定按钮
                int checknum=0;
                StringBuilder otherlabel=new StringBuilder();
                for(int i=0;i<malllabledatas.size();i++){
                    if(malllabledatas.get(i).ischecked()){
                        checknum++;
                        otherlabel.append(malllabledatas.get(i).getName());
                        otherlabel.append(",");
                    }
                }
                if(checknum==0){
                    showToast("请选择其他标签！");
                    return;
                }
                String otherLabel=otherlabel.deleteCharAt(otherlabel.length()-1).toString();//其他标签
                String belongLabel=null;//归属标签
                for(int i=0;i<mBelongLabledatas.size();i++){
                    if(mBelongLabledatas.get(i).ischecked()){
                        belongLabel=mBelongLabledatas.get(i).getName();
                    }
                }
                if(TextUtils.isEmpty(belongLabel)){
                    showToast("请先选择发布显示标签！");
                    return;
                }
                Utils.systemErr("belongLabel-->"+belongLabel);
                Utils.systemErr("otherLabel-->"+otherLabel);
                alterLable(belongLabel,otherLabel);
                break;
        }
    }
    /**
     * 修改标签
     * @param belongLabel
     * @param otherLabel
     */
    private  void alterLable(final String belongLabel, final String otherLabel){
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        Observable<ResultData<JsonObject>> observable = HttpManager.altLable(""+userid,belongLabel,otherLabel);
        observable.subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        showDialog();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        Intent intent=new Intent();
                        intent.putExtra("belongLabel",belongLabel);
                        intent.putExtra("otherLabel",otherLabel);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });

    }
}